<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="任务详情" prop="taskDescription">
        <el-input
          v-model="queryParams.taskDescription"
          placeholder="请输入任务详情"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发布时间" prop="taskStartTime">
        <el-date-picker clearable
          v-model="queryParams.taskStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="截止时间" prop="taskDeadlineTime">
        <el-date-picker clearable
          v-model="queryParams.taskDeadlineTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择截止时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属大学" prop="taskUniversity">
        <el-input
          v-model="queryParams.taskUniversity"
          placeholder="请输入所属大学"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['task:task:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['task:task:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['task:task:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['task:task:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="taskId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
<!--      <el-table-column label="分类名称" align="center" prop="taskCategoryId" />-->
      <el-table-column label="任务编号" align="center" prop="taskCode" />
      <el-table-column label="任务备注" align="center" prop="taskTitle" />
      <el-table-column label="任务详情" align="center" prop="taskDescription" />
      <el-table-column label="起始地址" align="center" prop="taskHomeaddressId" />
      <el-table-column label="终点地址" align="center" prop="taskGoaddressId" />
      <el-table-column label="任务价格" align="center" prop="taskReward" />
      <el-table-column label="任务状态" align="center" prop="taskStatus" />
      <el-table-column label="所属大学" align="center" prop="taskUniversity" />
      <el-table-column label="性别要求" align="center" prop="taskSexdemand" />
<!--      <el-table-column label="文件地址" align="center" prop="taskFileurl" />-->
      <el-table-column label="任务图片" align="center" prop="taskImageurl" width="100">
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.taskImageurl" :width="50" :height="50"/>-->
<!--        </template>-->
        <template slot-scope="scope">
          <div v-for="(imageUrl, index) in parseImageArray(scope.row.taskImageurl)" :key="index">
            <image-preview :src="imageUrl" :width="50" :height="50"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="物品数量" align="center" prop="taskGoodsNum" />
      <el-table-column label="物品体积" align="center" prop="taskSize" />
      <el-table-column label="发布时间" align="center" prop="taskStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.taskStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止时间" align="center" prop="taskDeadlineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.taskDeadlineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['task:task:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['task:task:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" disabled />
        </el-form-item>
        <el-form-item label="任务备注" prop="taskTitle">
          <el-input v-model="form.taskTitle" placeholder="请输入任务备注"/>
        </el-form-item>
        <el-form-item label="任务详情" prop="taskDescription">
          <el-input v-model="form.taskDescription" placeholder="请输入任务详情"/>
        </el-form-item>
        <el-form-item label="起始地址" prop="taskHomeaddressId">
          <el-input v-model="form.taskHomeaddressId" placeholder="请输入起始地址" disabled/>
        </el-form-item>
        <el-form-item label="终点地址" prop="taskGoaddressId">
          <el-input v-model="form.taskGoaddressId" placeholder="请输入终点地址" disabled/>
        </el-form-item>
        <el-form-item label="任务价格" prop="taskReward">
          <el-input v-model="form.taskReward" placeholder="请输入任务价格"/>
        </el-form-item>
        <el-form-item label="任务状态" prop="taskStatus">
          <el-input v-model="form.taskStatus" placeholder="任务状态" disabled/>
        </el-form-item>
        <el-form-item label="性别要求" prop="taskSexdemand">
<!--          <el-input v-model="form.taskSexdemand" placeholder="任务状态" disabled/>-->
          <el-select v-model="form.taskSexdemand" placeholder="性别要求">
            <el-option label="不限" value="不限"></el-option>
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="发布时间" prop="taskStartTime" disabled>-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.taskStartTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择发布时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="截止时间" prop="taskDeadlineTime" disabled>-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.taskDeadlineTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择截止时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="所属大学" prop="taskUniversity">
          <el-input v-model="form.taskUniversity" placeholder="请输入所属大学" disabled/>
        </el-form-item>
<!--        <el-form-item label="文件地址" prop="taskFileurl">-->
<!--          <el-input v-model="form.taskFileurl" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
        <el-form-item label="任务图片" prop="taskImageurl">
          <image-upload v-model="form.taskImageurl"/>
        </el-form-item>
        <el-form-item label="物品数量" prop="taskGoodsNum">
          <el-input v-model="form.taskGoodsNum" placeholder="请输入物品数量" />
        </el-form-item>
        <el-form-item label="物品体积" prop="taskSize">
          <el-select v-model="form.taskSize" placeholder="物品体积">
            <el-option label="小件" value="小件"></el-option>
            <el-option label="中件" value="中件"></el-option>
            <el-option label="大件" value="大件"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/task/task";

export default {
  name: "Task",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务管理表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        taskCategoryId: null,
        taskTitle: null,
        taskDescription: null,
        taskHomeaddressId: null,
        taskGoaddressId: null,
        taskReward: null,
        taskStatus: null,
        taskStartTime: null,
        taskDeadlineTime: null,
        taskUniversity: null,
        taskSexdemand: null,
        taskFileurl: null,
        taskImageurl: null,
        taskGoodsNum: null,
        taskSize: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseImageArray(imageString) {
      // 将后端返回的图片字符串转换成数组
      console.log(imageString,"12312")
      return imageString ? JSON.parse(imageString) : [];
    },
    /** 查询任务管理列表 */
    getList() {
      this.loading = true;
      //传递参数1，说明是取快递功能
      listTask(this.queryParams,1).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskId: null,
        userId: null,
        taskCategoryId: null,
        taskCode: null,
        taskTitle: null,
        taskDescription: null,
        taskHomeaddressId: null,
        taskGoaddressId: null,
        taskReward: null,
        taskStatus: null,
        taskStartTime: null,
        taskDeadlineTime: null,
        taskUniversity: null,
        taskSexdemand: null,
        taskFileurl: null,
        taskImageurl: null,
        taskGoodsNum: null,
        taskSize: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const taskId = row.taskId || this.ids
      getTask(taskId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.taskId != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskIds = row.taskId || this.ids;
      this.$modal.confirm('是否确认删除任务管理编号为"' + taskIds + '"的数据项？').then(function() {
        return delTask(taskIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('task/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
